package asteroids.statement;

import asteroids.*;
import asteroids.model.programs.*;

public class SeqStatement extends Statement {
	private Statement[] statements;

	public SeqStatement(Statement... statements) {
		int i = 0;
		while (i < statements.length) {
			if (statements[i] == null)
				statements[i] = new SkipStatement();
			i++;
		}
		this.statements = statements;
		this.stateNumber = 0;
		this.setBreaker(false);

	}
	public SeqStatement(){
		this.finished=true;
		this.statements= new Statement[0];
		this.stateNumber = 0;
	}

	public int getSize() {
		return statements.length;
	}

	@Override
	public void execute(Program program) {
		int i = 0;
		while (!this.finished && i < 10) {
			if (getStateNumber() > getSize() - 1) {
				this.finished = true;
			} else if (statements[getStateNumber()].isFinished()) {
				statements[getStateNumber()].loop();
				this.incrementStateNumber();
			} else {
				statements[getStateNumber()].execute(program);

				if ((statements[getStateNumber()].getType() == StatementType.ACTION)
						|| (statements[getStateNumber()].getBreaker())) {
					this.setBreaker(true);
					statements[getStateNumber()].setBreaker(false);
					break;
				}
			}
			i++;
		}
	}

}
